package com.sloan.community.controller;

import com.sloan.community.common.Login;
import com.sloan.community.common.query.SearchQuery;
import com.sloan.community.common.res.Result;
import com.sloan.community.common.vo.NotificationVO;
import com.sloan.community.common.vo.PageVo;
import com.sloan.community.po.User;
import com.sloan.community.service.NotificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@Slf4j
@Controller
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
@RequestMapping("notice")
public class NotificationController {


    private final NotificationService notificationService;


    @Login
    @GetMapping("list")
    public Mono<String> noticePageList(SearchQuery query, User user, Model model) {
        return notificationService.noticePageList(query, user.getId())
                .map(page -> {
                    model.addAttribute("notification", page);
                    return "notification";
                });
    }


    @Login
    @ResponseBody
    @PutMapping("/{notificationId}/{status}")
    public Mono<Result<Long>> noticeStatus(@PathVariable("notificationId") Long notificationId,
                                           @PathVariable("status") @Range(min = 0, max = 1, message = "参数非法") Integer status,
                                           User user) {
        return notificationService.noticeStatus(notificationId, status, user.getId())
                .map(Result::success);
    }

    @Login
    @ResponseBody
    @GetMapping("unread")
    public Mono<Result<Long>> unread(User user) {
        return notificationService.unread(user.getId())
                .map(Result::success);
    }

}
